﻿// Copyright (c) 2009 Konstantin Yegupov
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

TVWindow = function(x, y, w, h, back, col, title) {
    if (col==undefined) col = 'white';
    this.block = new Block(x,y,w,h,back);
    this.col = col;
    this.title = title;
    this.resize(x,y,w,h);
}

TVWindow.prototype.resize = function(x, y, w, h) {
    this.block.x = x;
    this.block.y = y;
    this.block.w = w;
    this.block.h = h;
    this.buildFrame();
}

lines_dbl = '═║╔╗╚╝';
lines_sgl = '─│┌┐└┘';

TVWindow.prototype.buildFrame = function() {
    var b = this.block; 
    var els2 = [];
    for (var i=0; i<this.block.els.length; i++) {
        if (b.els[i].is_wnd_base===undefined) els2.push(b.els[i]);
    }
    b.els = els2;
    var lines = (this.is_active) ? lines_dbl : lines_sgl;
    var hm = this.h_margin;
    if (hm==undefined) hm=0;
    if (this.title!=undefined) {
        b.add_base(new Element(hm, 0, lines[2] + strdup(lines[0], Math.ceil((b.w-this.title.length-4-2*hm)/2)) + ' ' + this.title + ' '+ strdup(lines[0], Math.floor((b.w-this.title.length-4-2*hm)/2)) + lines[3], this.col, b.back));
    } else {
        b.add_base(new Element(hm, 0, lines[2] + strdup(lines[0], b.w-2-2*hm) + lines[3], this.col, b.back));
    }
    for (var i=1; i<b.h-1; i++) {
        b.add_base(new Element(hm, i, lines[1], this.col, b.back));
        b.add_base(new Element(b.w-1-hm, i, lines[1], this.col, b.back));
    }
    b.add_base(new Element(hm, b.h-1, lines[4] + strdup(lines[0], b.w-2-2*hm) + lines[5], this.col, b.back));
}

Block.prototype.add_base = function(el) {
    el.block = this;
    el.is_wnd_base = true;
    this.els.push(el);
}

MenuWindows = [];
MenuWindows.provide_blocks = function() {
    var res = [];
    for (var i=0; i<MenuWindows.length; i++) {
        if (MenuWindows[i].visible) {
            res.push(MenuWindows[i].window.block);
        }
    }
    return res;
}

MenuBars = [];

MenuBar = function(x,y,w) {
    this.pos = 1;
    this.block = new Block(x,y,w,1, '#aaa');
    MenuBars.push(this);
}

MenuBar.prototype.add = function(text, onclick) {
    var el = new Element(this.pos, 0, ' '+text+' ', '#000', '#aaa');
    this.pos += 2 + text.length;
    el.onclick = function(ev){onclick(); this.tmel.back = '#0a0'; Renderer.go()}
    this.block.add(el);
}

MenuBar.prototype.provide_blocks = function() {
    return [this.block];
}

MenuDrop = function(x,y,w) {
    this.window = new TVWindow(x,y,w,2,'#aaa', '#000');
    this.window.h_margin = 1;
    MenuWindows.push(this);
}

MenuDrop.prototype.add = function(text, onclick) {
    var el = new Element(3, this.window.block.h-1, ' '+text+' ', '#000', '#aaa');
    el.onclick = function(ev){onclick(); this.tmel.back = '#0a0'; Renderer.go()}
    this.window.block.add(el);
    this.window.block.h+=1;
    this.window.buildFrame();
}

MenuDrop.prototype.add_splitter = function(text, onclick) {
    var el = new Element(1, this.window.block.h-1, '│'+strdup('─',this.window.block.w-4)+'│', '#000', '#aaa');
    this.window.block.add(el);
    this.window.block.h+=1;
    this.window.buildFrame();
}

MenuDrop.prototype.provide_blocks = function() {
    return [this.window.block];
}



TextMode.defaultClick = function() {
    for (var i=0; i<MenuWindows.length; i++) {
        MenuWindows[i].visible = false;
    }
    for (i=0; i<MenuBars.length; i++) {
        for (var j=0; j<MenuBars[i].block.els.length; j++) {
            MenuBars[i].block.els[j].back = '#aaa';
        }
    }
    Renderer.go();
}



